package org.cjlm.muaction.frm;

import org.cjlm.ui.pub.MAbstraction;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class FrameGridAction extends MAbstraction {
    public FrameGridAction(String title) {
        super(title);
    }
    @Override
    public void doAction(ActionEvent e) throws Exception {
        arrangeFrames();
    }

    public void arrangeFrames() {
        JInternalFrame[] frames = getDesktop().getAllFrames();
        // 使用平铺算法
        int cols = (int) Math.ceil(Math.sqrt(frames.length));
        int width = getDesktop().getWidth() / cols;

        for(int i=0; i<frames.length;  i++){
            int row = i / cols;
            int col = i % cols;
            frames[i].setSize(width, 300);
            frames[i].setLocation(col * width, row * 310);
        }
    }
}
